package org.hin.programmering1;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.util.LinkedList;

import org.hin.programmering1.engine.AnimatedSprite;
import org.hin.programmering1.engine.Input;
import org.hin.programmering1.engine.Sprite;

/**
 * TestWorld
 * For testing the features of the engine
 * 
 * @author Meanz, Fredrik
 */
public class TestWorld {

	Sprite mainCharacter;
	
	AnimatedSprite testAnimation;
	AnimatedSprite bullet;
	/**
	 * Load sprites and shit here
	 */
	public void gameLoad() {
		mainCharacter = Game.addSprite("./resources/orange.png", 200, 50);
		testAnimation = Game.addAnimation(300, 300, 0.5f,
				"./resources/anim1.png", 
				"./resources/anim2.png", 
				"./resources/anim3.png", 
				"./resources/anim4.png", 
				"./resources/anim5.png", 
				"./resources/anim6.png");
		bullet = Game.addAnimation(0, 0, 0.5f,
				"./resources/bullet/b1.png", 
				"./resources/bullet/b2.png");
	}
	
	/**
	 * Happends when the game updates
	 */
	public void gameUpdate() {
		if(Input.isKeyDown(KeyEvent.VK_H)) {
			mainCharacter.setImage(null);
		}
		
		int moveSpeed = 4;
		Point lastPosition = new Point(testAnimation.transform.position.x, testAnimation.transform.position.y);
		if(Input.isKeyDown(KeyEvent.VK_ESCAPE)) {
			System.exit(0);
		}
		if(Input.isKeyDown(KeyEvent.VK_UP)) {
			testAnimation.transform.translate(0, -moveSpeed);
		}
		if(Input.isKeyDown(KeyEvent.VK_DOWN)) {
			testAnimation.transform.translate(0, moveSpeed);
		}
		if(Input.isKeyDown(KeyEvent.VK_LEFT)) {
			testAnimation.transform.translate(-moveSpeed, 0);
		}
		if(Input.isKeyDown(KeyEvent.VK_RIGHT)) {
			testAnimation.transform.translate(moveSpeed, 0);
		}
		if(Input.isKeyDown(KeyEvent.VK_SPACE)) {
			bullet.transform.setPosition(lastPosition);
		}
		
		if(testAnimation.transform.position.x <= 0
				|| testAnimation.transform.position.y <= 25
				|| testAnimation.transform.position.x >= 800 - (testAnimation.getImage().getWidth(null))
				|| testAnimation.transform.position.y >= 640 - (testAnimation.getImage().getHeight(null))) {
			testAnimation.transform.position = lastPosition;
		}
		
		mainCharacter.transform.setPosition(Input.getMouseX(), Input.getMouseY());
	}
	
	/**
	 * Happends when the game renders a frame
	 */
	public void gameRender(Graphics g) {
		g.setColor(Color.yellow);
		g.drawString("Overtime cycles: " + Game.getFPS(), 10, 45);
	}
}

